package io.renren.com.qywt.controller;

import com.alibaba.excel.EasyExcel;
import io.renren.com.qywt.excel.listener.AftersaleReturnGoodExcelListener;
import io.renren.com.qywt.service.AftersaleMultiNumService;
import io.renren.commons.log.annotation.LogOperation;
import io.renren.commons.tools.constant.Constant;
import io.renren.commons.tools.page.PageData;
import io.renren.commons.tools.utils.Result;
import io.renren.commons.tools.utils.ExcelUtils;
import io.renren.commons.tools.validator.AssertUtils;
import io.renren.commons.tools.validator.ValidatorUtils;
import io.renren.commons.tools.validator.group.AddGroup;
import io.renren.commons.tools.validator.group.DefaultGroup;
import io.renren.commons.tools.validator.group.UpdateGroup;
import io.renren.com.qywt.dto.AftersaleReturnGoodDTO;
import io.renren.com.qywt.excel.AftersaleReturnGoodExcel;
import io.renren.com.qywt.service.AftersaleReturnGoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
* 仓库退货表
*
* @author jiaohj 15110414840@139.com
* @since 3.0 2022-09-22
*/
@RestController
@RequestMapping("return")
@Api(tags="仓库退货表")
public class AftersaleReturnGoodController {
    @Autowired
    private AftersaleReturnGoodService aftersaleReturnGoodService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @PreAuthorize("hasAuthority('aftersale:return:page')")
    public Result<PageData<AftersaleReturnGoodDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<AftersaleReturnGoodDTO> page = aftersaleReturnGoodService.page(params);

        return new Result<PageData<AftersaleReturnGoodDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @PreAuthorize("hasAuthority('aftersale:return:info')")
    public Result<AftersaleReturnGoodDTO> get(@PathVariable("id") Long id){
        AftersaleReturnGoodDTO data = aftersaleReturnGoodService.get(id);
        return new Result<AftersaleReturnGoodDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @PreAuthorize("hasAuthority('aftersale:return:save')")
    public Result save(@RequestBody AftersaleReturnGoodDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        aftersaleReturnGoodService.save(dto);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @PreAuthorize("hasAuthority('aftersale:return:update')")
    public Result update(@RequestBody AftersaleReturnGoodDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        aftersaleReturnGoodService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @PreAuthorize("hasAuthority('aftersale:return:delete')")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        aftersaleReturnGoodService.delete(ids);
        return new Result();
    }

    @PostMapping("import")
    @ApiOperation("导入")
    @PreAuthorize("hasAuthority('aftersale:return:import')")
    @ApiImplicitParam(name = "file", value = "文件", paramType = "query", dataType="file")
    public Result importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        //解析并保存到数据库
        EasyExcel.read(file.getInputStream(), AftersaleReturnGoodExcel.class, new AftersaleReturnGoodExcelListener(aftersaleReturnGoodService)).sheet().doRead();
        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @PreAuthorize("hasAuthority('aftersale:return:export')")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AftersaleReturnGoodDTO> list = aftersaleReturnGoodService.list(params);
        ExcelUtils.exportExcelToTarget(response, null, "仓库退货表", list, AftersaleReturnGoodExcel.class);
    }

}